#pragma once
#include "util/mutex.h"
#include "base/config.h"

#include <stdio.h>
#include <string>
namespace net_tools
{
  class Eventloop;
  namespace base
  {
    class Channel;
    namespace detail
    {
      struct Buffer_main;
    } // namespace detail
    class Channel;
    class Logfile;
    class Logbuffer
    {
    public:
      Logbuffer(Logfile *logfile, net_tools::Eventloop *eventloop);
      ~Logbuffer();
      void Set_fd(FILE *fd_stream);
      size_t Flush();
      void Output(void *address, short int len);

    private:
      void Call_thread_to_flush();
      Logfile *logfile_;
      net_tools::Eventloop *eventloop_;
      FILE *fd_stream_;
      detail::Buffer_main *p_now_buffer_;
      detail::Buffer_main *p_flush_buffer_;
      volatile bool is_swap_;
      bool need_change_file_;
      FILE *new_fd_stream_;
      util::Mutex mutex_;
    };

  } // namespace base
} // namespace net_tools